package com.zjitc.controller;

import com.github.pagehelper.PageInfo;
import com.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create By IntelliJ IDEA
 *
 * @author:
 * @create-Time: 2017/11/15  14:51
 */
@Controller
@RequestMapping("/product")
public class ProductController {

  private final int COUNT = 12;

  private final ProductService service;

  @Autowired
  public ProductController(ProductService service) {this.service = service;}

  @RequestMapping(value = "/displaymsg", produces = "application/json;charset=UTF-8")
  public ModelAndView displaymsg(HttpSession session, String pid)
      throws ServletException, IOException {
    return service.displaymsg(session, pid);
  }

  @RequestMapping("/list")
  public ModelAndView list(@RequestParam("cid") String cid,
      @RequestParam(value = "pageid", required = false, defaultValue = "0") Integer pageid)
      throws ServletException, IOException {
    PageInfo info = service.findall(cid, pageid, COUNT);
    ModelAndView mv = new ModelAndView();
    mv.addObject("page", info);
    mv.addObject("cid", cid);
    mv.setViewName("product_list");
    return mv;
  }
}
